package edu.suda.xsap.controller;

import edu.suda.xsap.common.Result;
import edu.suda.xsap.dto.CourseDto;
import edu.suda.xsap.dto.EmployeeDto;
import edu.suda.xsap.entity.Course;
import edu.suda.xsap.entity.ReservationSet;
import edu.suda.xsap.entity.vo.*;
import edu.suda.xsap.service.CourseService;
import edu.suda.xsap.service.MemberCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course-management")
@Slf4j
public class CourseController {
@Autowired
private CourseService courseService;
@Autowired
private MemberCardService memberCardService;

    /**'/api/course-management/get-res-records', {"id": id})*/
    @PostMapping ("/get-res-records")
    public Result getResRecords(@RequestBody IdVo idvo) {
        try {
            List<ReservationVo> reservationVos = courseService.find(idvo.getId());
            System.out.println(reservationVos);
            return Result.success(reservationVos);
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }
    /**'/api/course-management/get-res-records', {"id": id}).**/
    @PostMapping("/get-class-records")
    public Result getClassRecords(@RequestBody IdVo idvo) {
        try {
            List<ClassRecordVo> lst= courseService.findClassRecord(idvo.getId());
            System.out.println(lst);
            return Result.success(lst);
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }

    /**'/api/course-management/course-reservation/.**/
    @PostMapping("/course-reservation")
    public Result courseReservation(@RequestBody ReservationSet reservationSet) {
        try {
            courseService.saveReservation(reservationSet);
            return Result.success("创建成功");
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }


    /***course-management/course-list/getlength***/
    @GetMapping("/course-list/getlength")
    public Result getlength() {
        try {
            Integer count = courseService.findCount();
            return Result.success(count);
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }


    /***course-management/course-list/getOnePageDate***/
    @PostMapping("/course-list/getOnePageDate")
    public Result getOnePageDate(@RequestBody Page pages) {
        try {
            System.out.println(pages);
            Integer page=pages.getPage();
            System.out.println(page);
            List<CourseDto> lst = courseService.findByLimit(10, (page - 1) * 10);
            System.out.println(lst);
            return Result.success(lst);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }


    /***course-management/get-courses**/
    @GetMapping("/get-courses")
    public Result getCourses() {
        try {
            List<CourseDto> lst = courseService.findAll();
            System.out.println(lst);
            return Result.success(lst);
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }


    /***'/course-management/get-card-course***/
    @PostMapping ("/get-card-course")
    public Result getCardCourse(@RequestBody IdVo idvo) {
        try {
            Integer id = idvo.getId();
            memberCardService.findId(id);
            CardNewVo lst= memberCardService.findId(id);
            return Result.success(lst);
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }



}
